#include "StdFile.h"
#include <stdio.h>
#include <stdbool.h>

MyStr ReadFromFile(const char *filePath)
{   
    INITSTR(s);
    FILE *f = fopen(filePath,"r");
    if(f == NULL)
    {
        printf("文件打开失败:%d\n",ferror(f));
        return s;
    }

    fseek(f,0,SEEK_END);
    long FileSize = ftell(f);
    rewind(f);

    ResizeSting(&s,FileSize+1);  
    if(fread(s.string,FileSize,1,f) ==0 && feof(f) != true)
    {
        printf("文件读取错误:%d\n",ferror(f));
        ClearString(&s);
        return s;
    }
    fclose(f);
    return s;
}

MyStrList GetLineFromFile(const char *filePath)
{
    
}

bool AppendToFile(const char *filePath, const void *content, size_t size)
{   
     FILE *f = fopen(filePath,"a");
    if(f == NULL)
    {
        printf("open errror\n");
        return false;
    }
    MyStrList list;
    InitMyStringList(&list);

    FILE *f = fopen(filePath,"r");
    if(f == NULL)
    {
        printf("文件打开失败:%d\n",ferror(f));
        return false;
    }

    char c = 0;
    while((c = fgetc(f)) != EOF)
    {
        INITSTR(str);
        while (c != '\n' && c != EOF)
        {
            InsertChar(&str,c,str.len);
            c = fgetc(f);
        }
        if(str.len != 0)
        {
              listInsertTail(&list,str.string);
              ClearString(&str);
        }
       fclose(f);
       return true;
        
    }


}

bool WriteToFile(const char *filePath,const void *content,size_t size)
{   
    FILE *f = fopen(filePath,"r");
    if(f == NULL)
    {
        printf("文件打开失败:%d\n");
        return false;
    }

    if(fwrite(content,size,1,f) ==0)
    {
        printf("write error:%d\n",ferror(f));
        fclose(f);
        return false;
    }
    fclose(f);
    return true;
}

bool AppendToFile(const char *filePath, const void *content, size_t size)
{   
     FILE *f = fopen(filePath,"a");
    if(f == NULL)
    {
        printf("文件打开失败:%d\n");
        return false;
    }

    if(fwrite(content,size,1,f) ==0)
    {
        printf("write error:%d\n",ferror(f));
        fclose(f);
        return false;
    }
    fclose(f);
    return true;   
}

bool WriteStringListToFile(const char *filePath, MyStrList strlist)
{   
    FILE *f = fopen(filePath,"a");
    if(f == NULL)
    {
        printf("文件打开失败:%d\n");
        return false;
    }
    
    for(int i = 0;i < strlist.len;i++)
    {
        MyStr s = strlist.stringList[i];
        InsertString(&s,"\n");
        if(fwrite(s.string,s.len,1,f) == 0)
        {
            printf("write error:%d\n",ferror(f));
            fclose(f);
            return false;
        }
    }
    fclose(f);
    return true;
}

size_t GetFileSize(const char *filePath)
{   
    FILE *f = fopen(filePath,"r");
    if(f == NULL)
    {
        printf("文件打开失败:%d\n",ferror(f));
        return -1;
    }

    fseek(f,0,SEEK_END);
    long FileSize = ftell(f);
    fclose(f);
    return FileSize;
}

void CreateNewFile(const char *filePath)
{  //文件里面得到一行内容的函数
    FILE *f=fopen(filePath,"w");
    if(f == NULL)
    {
        return;
    }
    fclose(f);
}

bool IsFileExist(const char *filePath)
{   
     FILE *f=fopen(filePath,"r");
    if(f == NULL)
    {
        false;
    }
    fclose(f);
    return true;
}
